package code {

import flash.net.SharedObject;
	

public class Cookie {
	
	private var so: SharedObject;

	public function Cookie(singletonEnforcer: SingletonEnforcer) {
		this.so = SharedObject.getLocal("jsugvoting_cookie");
	}
	
	private function getSafeValue(key: String, defaultValue: Object): Object {
		const stored: Object = this.so.data[key];
		if(stored != null) {
			return stored;
		}
		
		this.so.data[key] = defaultValue;
		this.flush();
		
		return defaultValue;
	}
	
	public function clearDataAndFlush() {
		this.username = '';
		this.password = '';
		this.userId = -1;
		this.isRememberEnabled = false;
		this.flush();
	}
	
	public function get isRememberEnabled(): Boolean {
		return this.getSafeValue('isRememberEnabled', false) as Boolean;
	}
	
	public function set isRememberEnabled(value: Boolean): void {
		this.so.data.isRememberEnabled = value;
	}
	
	public function get username(): String {
		return this.getSafeValue('username', '') as String;
	}
	
	public function set username(value: String): void {
		this.so.data.username = value;
	}
	
	public function get password(): String {
		return this.getSafeValue('password', '') as String;
	}
	
	public function set password(value: String): void {
		this.so.data.password = value;
	}
	
	public function get userId(): int {
		return this.getSafeValue('userId', -42) as int;
	}
	
	public function set userId(value: int): void {
		this.so.data.userId = value;
	}
	
	
	public function flush(): void {
		this.so.flush();
	}
 	
	private static var INSTANCE: Cookie;
	public static function get instance(): Cookie {
		if(INSTANCE == null) { INSTANCE = new Cookie(new SingletonEnforcer()); }
		return INSTANCE;
	}
}
}

class SingletonEnforcer { }